package com.lanou3g.baidumusic.main.musicfragment.recommendfragment;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lanou3g.baidumusic.R;
import com.squareup.picasso.Picasso;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 17/3/23.
 */

public class RecommendRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{



    private Context context ;
    private RecommendRecyclerViewBean bean ;


    private static final int TYPE_TITLE = 0 ;
    private static final int TYPE_ONE   = 1 ;
    private static final int TYPE_TWO   = 2 ;
    private static final int TYPE_THREE = 3 ;
    private static final int TYPE_FOUR  = 4 ;
    private static final int TYPE_FIVE  = 5 ;
    private static final int TYPE_SIX   = 6 ;
    private static final int TYPE_SEVEN = 7 ;
    private static final int TYPE_EIGHT = 8 ;
    private static final int TYPE_NINE  = 9 ;
    private static final int TYPE_TEN   = 10 ;

    private RecyclerViewTwoAdapter recyclerViewTwoAdapter ;
    private RecyclerViewThreeAdapter recyclerViewThreeAdapter ;
    private RecyclerViewSixAdapter recyclerViewSixAdapter ;
    private List<String> url ;

    public RecommendRecyclerViewAdapter(Context context) {
        this.context = context;
        notifyDataSetChanged();
    }
    public void setRecommendRecyclerViewBean(RecommendRecyclerViewBean bean){
        this.bean = bean ;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder = null ;
        switch (viewType){
            case TYPE_TITLE:
                View title = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_rv_title, parent ,false);
                Banner banner = (Banner) title.findViewById(R.id.item_fragment_recommend_banner);
                banner.setImageLoader(new MyImageLoader());
                url = new ArrayList<>();
                for (int i = 0; i < bean.getResult().getFocus().getResult().size(); i++) {
                    url.add(bean.getResult().getFocus().getResult().get(i).getRandpic());
                    Log.d("RecommendRecyclerViewAd", bean.getResult().getFocus().getResult().get(i).getRandpic());
                }
//                for (int i = 0; i < url.size(); i++) {
//                    Log.d("RecommendRecyclerViewAd", url.get(i));
//                }
                banner.setImages(url);
                banner.start();



                viewHolder = new TitleHolder(title);
                break;
            case TYPE_ONE:
                View one = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_one,parent , false);
                viewHolder = new OneHolder(one);
                break;
            case TYPE_TWO:
                View two = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_two,parent , false);
                viewHolder = new TwoHolder(two);
                break;
            case  TYPE_THREE:
                View three = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_three , parent ,false);
                viewHolder = new ThreeHolder(three);
                break;
            case  TYPE_FOUR:
                View four = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_three , parent ,false);
                viewHolder = new ThreeHolder(four);
                break;
            case  TYPE_FIVE:
                View five = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_five , parent , false);
                viewHolder = new FiveHolder(five);
                break;
            case  TYPE_SIX:
                View six = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_three , parent ,false);
                viewHolder = new ThreeHolder(six);
                break;
            case  TYPE_SEVEN:
                View seven = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_two , parent ,false);
                viewHolder = new TwoHolder(seven);
                break;
            case  TYPE_EIGHT:
                View eight = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_three , parent ,false);
                viewHolder = new ThreeHolder(eight);
                break;
            case  TYPE_NINE:
                View nine = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_two , parent ,false);
                viewHolder = new TwoHolder(nine);
                break;
            case  TYPE_TEN:
                View ten = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_three , parent ,false);
                viewHolder = new ThreeHolder(ten);
                break;
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        switch (position){
            case TYPE_TITLE:


                break;
            case TYPE_ONE:

                break;

            case TYPE_TWO:
                TwoHolder twoHolder = (TwoHolder) holder;

                GridLayoutManager gridLayoutManager2 = new GridLayoutManager(context, 3 , LinearLayoutManager.VERTICAL,false);
                twoHolder.recyclerView.setLayoutManager(gridLayoutManager2);

                recyclerViewTwoAdapter = new RecyclerViewTwoAdapter(context , 2);
                twoHolder.recyclerView.setAdapter(recyclerViewTwoAdapter);
                recyclerViewTwoAdapter.setRecommendTwoRecyclerViewBean(bean);
                Log.d("onBindViewHolder", "onBindViewHolder: 2");

                break;

            case TYPE_THREE:
                ThreeHolder threeHolder = (ThreeHolder) holder;
                Log.d("onBindViewHolder", "onBindViewHolder: 3");
                GridLayoutManager gridLayoutManager3 = new GridLayoutManager(context, 3 , LinearLayoutManager.VERTICAL,false);
                threeHolder.recyclerView.setLayoutManager(gridLayoutManager3);

                recyclerViewThreeAdapter = new RecyclerViewThreeAdapter(context , 3);
                threeHolder.recyclerView.setAdapter(recyclerViewThreeAdapter);
                recyclerViewThreeAdapter.setRecommendThreeRecyclerViewBean(bean);
                break;

            case TYPE_FOUR:
                ThreeHolder fourHolder = (ThreeHolder) holder;
                GridLayoutManager gridLayoutManager4 = new GridLayoutManager(context, 3 , LinearLayoutManager.VERTICAL,false);
                fourHolder.recyclerView.setLayoutManager(gridLayoutManager4);

                recyclerViewThreeAdapter = new RecyclerViewThreeAdapter(context , 4);
                fourHolder.recyclerView.setAdapter(recyclerViewThreeAdapter);
                recyclerViewThreeAdapter.setRecommendThreeRecyclerViewBean(bean);
                break;

            case TYPE_FIVE:


                break;

            case TYPE_SIX:
                ThreeHolder sixHolder = (ThreeHolder) holder;
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context , LinearLayoutManager.VERTICAL,false);
                sixHolder.recyclerView.setLayoutManager(linearLayoutManager);

                recyclerViewSixAdapter = new RecyclerViewSixAdapter(context , 6);
                sixHolder.recyclerView.setAdapter(recyclerViewSixAdapter);
                recyclerViewSixAdapter.setRecommendSixRecyclerViewBean(bean);
                break;

            case TYPE_SEVEN:
                TwoHolder sevenHolder = (TwoHolder) holder;
                GridLayoutManager gridLayoutManager7 = new GridLayoutManager(context, 3 , LinearLayoutManager.VERTICAL,false);
                sevenHolder.recyclerView.setLayoutManager(gridLayoutManager7);

                recyclerViewTwoAdapter = new RecyclerViewTwoAdapter(context , 7);
                sevenHolder.recyclerView.setAdapter(recyclerViewTwoAdapter);
                recyclerViewTwoAdapter.setRecommendTwoRecyclerViewBean(bean);
                break;

            case TYPE_EIGHT:
                ThreeHolder eightHolder = (ThreeHolder) holder;

                GridLayoutManager gridLayoutManager8 = new GridLayoutManager(context, 3 , LinearLayoutManager.VERTICAL,false);
                eightHolder.recyclerView.setLayoutManager(gridLayoutManager8);

                recyclerViewThreeAdapter = new RecyclerViewThreeAdapter(context , 8);
                eightHolder.recyclerView.setAdapter(recyclerViewThreeAdapter);
                recyclerViewThreeAdapter.setRecommendThreeRecyclerViewBean(bean);
                break;
            case TYPE_NINE:
                TwoHolder nineHolder = (TwoHolder) holder;

                GridLayoutManager gridLayoutManager9 = new GridLayoutManager(context, 3 , LinearLayoutManager.VERTICAL,false);
                nineHolder.recyclerView.setLayoutManager(gridLayoutManager9);

                recyclerViewTwoAdapter = new RecyclerViewTwoAdapter(context , 9);
                nineHolder.recyclerView.setAdapter(recyclerViewTwoAdapter);
                recyclerViewTwoAdapter.setRecommendTwoRecyclerViewBean(bean);
                break;
            case TYPE_TEN:
                ThreeHolder tenHolder = (ThreeHolder) holder;

                LinearLayoutManager linearLayoutManager10 = new LinearLayoutManager(context , LinearLayoutManager.VERTICAL,false);
                tenHolder.recyclerView.setLayoutManager(linearLayoutManager10);

                recyclerViewSixAdapter = new RecyclerViewSixAdapter(context , 10);
                tenHolder.recyclerView.setAdapter(recyclerViewSixAdapter);
                recyclerViewSixAdapter.setRecommendSixRecyclerViewBean(bean);
                break;
        }



    }

    @Override
    public int getItemViewType(int position) {
        switch (position){

            case 0:
                return TYPE_TITLE;
            case 1:
                return TYPE_ONE;
            case 2:
                return TYPE_TWO ;
            case 3:
                return TYPE_THREE ;
            case 4:
                return TYPE_FOUR ;
            case 5:
                return TYPE_FIVE ;
            case 6:
                return TYPE_SIX ;
            case 7:
                return TYPE_SEVEN ;
            case 8:
                return TYPE_EIGHT ;
            case 9:
                return TYPE_NINE ;
            case 10:
                return TYPE_TEN ;

        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {

        if (null == bean){
            return 0 ;
        }
        if (null == bean.getResult()){
            return 0 ;
        }

        return bean.getResult() != null  ? 11 : 0;
    }












    private class TitleHolder extends RecyclerView.ViewHolder{


        public TitleHolder(View itemView) {
            super(itemView);
        }
    }


    private class OneHolder extends RecyclerView.ViewHolder{
        ImageView picOneIv ,picTwoIv , picThreeIv ,  picFourIv ,picPicIv ;

        public OneHolder(View itemView) {
            super(itemView);
            picPicIv = (ImageView) itemView.findViewById(R.id.item_fragment_recommend_two_first_pic_iv);
        }
    }



    private class TwoHolder extends RecyclerView.ViewHolder{

        private RecyclerView recyclerView ;
        private TextView textView ;

        public TwoHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.item_fragment_recommend_two_tv);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.item_fragment_recommend_two_rv);
        }
    }


    private class ThreeHolder extends RecyclerView.ViewHolder{

        private RecyclerView recyclerView ;
        private TextView textView ;

        public ThreeHolder(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.item_fragment_recommend_three_rv);
            textView = (TextView) itemView.findViewById(R.id.item_fragment_recommend_three_tv);


        }
    }
    private class FiveHolder extends RecyclerView.ViewHolder{



        public FiveHolder(View itemView) {
            super(itemView);

        }
    }

    public class MyImageLoader extends ImageLoader{

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).crossFade().into(imageView);
        }
    }


}
